package com.sugree.twitter.views;

import java.io.IOException;
import java.util.Vector;
import java.util.Enumeration;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.rms.RecordStoreException;

import com.substanceofcode.twitter.model.Status;
import com.substanceofcode.twitter.Settings;
import com.substanceofcode.twitter.TwitterApi;
import com.substanceofcode.utils.Log;
import com.sugree.twitter.TwitterController;
import com.sugree.twitter.views.TimelineScreen;

public class SetupScreen extends Form implements CommandListener {
	private TwitterController controller;
	private TwitterApi api;
	private TimelineScreen timeline;

	private TextField usernameField;
	private TextField passwordField;
	private TextField timelineLengthField;
	private TextField gatewayField;
	private Command saveCommand;
	private Command cancelCommand;
	private Command gatewayCommand;
	private Command gatewayTwitterCommand;
	private Command gatewayBirdnestApiCommand;
	private Command gatewayBirdnestOptimizedCommand;
	private Command gatewayBirdnestImageCommand;
	private Command gatewayBirdnestTextCommand;

	public SetupScreen(TwitterController controller, TwitterApi api, TimelineScreen timeline) {
		super("Setup");
		this.controller = controller;
		this.api = api;
		this.timeline = timeline;

		Settings settings = controller.getSettings();

		String username = settings.getStringProperty(Settings.USERNAME, "");
		usernameField = new TextField("Username", username, 32, TextField.ANY);
		append(usernameField);

		String password = settings.getStringProperty(Settings.PASSWORD, "");
		passwordField = new TextField("Password", password, 32, TextField.PASSWORD);
		append(passwordField);

		String gateway = settings.getStringProperty(Settings.GATEWAY, "http://twitter.com/");
		gatewayField = new TextField("Gateway", gateway, 128, TextField.URL);
		append(gatewayField);

		int timelineLength = settings.getIntProperty(Settings.TIMELINE_LENGTH, 20);
		timelineLengthField = new TextField("Timeline Length", String.valueOf(timelineLength), 32, TextField.NUMERIC);
		append(timelineLengthField);

		saveCommand = new Command("Save", Command.OK, 1);
		addCommand(saveCommand);
		cancelCommand = new Command("Cancel", Command.CANCEL, 2);
		addCommand(cancelCommand);

		gatewayTwitterCommand = new Command("Gateway Twitter", Command.SCREEN, 10);
		addCommand(gatewayTwitterCommand);
		gatewayBirdnestApiCommand = new Command("Gateway Birdnest API", Command.SCREEN, 11);
		addCommand(gatewayBirdnestApiCommand);
		gatewayBirdnestOptimizedCommand = new Command("Gateway Birdnest Optimized", Command.SCREEN, 12);
		addCommand(gatewayBirdnestOptimizedCommand);
		gatewayBirdnestImageCommand = new Command("Gateway Birdnest Image", Command.SCREEN, 13);
		addCommand(gatewayBirdnestImageCommand);
		gatewayBirdnestTextCommand = new Command("Gateway Birdnest Text", Command.SCREEN, 14);
		addCommand(gatewayBirdnestTextCommand);

		setCommandListener(this);
	}

	public void commandAction(Command cmd, Displayable display) {
		if (cmd == saveCommand) {
			String username = usernameField.getString();
			String password = passwordField.getString();
			String gateway = gatewayField.getString();
			int timelineLength = Integer.parseInt(timelineLengthField.getString());

			if (!gateway.endsWith("/")) {
				gateway += "/";
			}

			Settings settings = controller.getSettings();

			settings.setStringProperty(Settings.USERNAME, username);
			settings.setStringProperty(Settings.PASSWORD, password);
			settings.setStringProperty(Settings.GATEWAY, gateway);
			settings.setIntProperty(Settings.TIMELINE_LENGTH, timelineLength);

			api.setGateway(gateway);
			api.setUsername(username);
			api.setPassword(password);
			timeline.setLength(timelineLength);

			try {
				settings.save(true);
			} catch (IOException e) {
				Log.error(e.toString());
			} catch (RecordStoreException e) {
				Log.error(e.toString());
			}
			controller.showTimeline();
		} else if (cmd == cancelCommand) {
			controller.showTimeline();
		} else if (cmd == gatewayTwitterCommand) {
			gatewayField.setString("http://twitter.com/");
		} else if (cmd == gatewayBirdnestApiCommand) {
			gatewayField.setString("http://nest.appspot.com/api/");
		} else if (cmd == gatewayBirdnestOptimizedCommand) {
			gatewayField.setString("http://nest.appspot.com/optimized/");
		} else if (cmd == gatewayBirdnestImageCommand) {
			gatewayField.setString("http://nest.appspot.com/image/");
		} else if (cmd == gatewayBirdnestTextCommand) {
			gatewayField.setString("http://nest.appspot.com/text/");
		}
	}
}
